package com.zty.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

/**
 * @author tyzhang19
 * @Description 处理json数据
 * @date 2022-10-14 14:54
 */
@Component
public class JsonHandleUtil {
//    final String json = "{\"objects\" : [\"One\", \"Two\", \"Three\"]}";
    final String json = "{\"objects\" : [{\"url\" : \"www\", \"lan\" : \"USA\"}, {\"url\" : \"com\", \"lan\" : \"RSA\"}]}";

    final String json1 = "{\"icons\" : { \"300*300\" : [{ \"url\": \"https://www.baidu.com\", \"language\": \"HI\" }] }}";

    final String json2 = "{\"icons\" : { \"300*300\" : { \"url\": \"https://www.baidu.com\" }, \"400*300\" : { \"url\": \"https://www.baidu.com\", \"lan\" : \"USA\" }, \"400*400\" : { \"url\": \"https://www.baidu.com\" } }}";


    public void handler() throws JsonProcessingException {
        JsonNode node = new ObjectMapper().readValue(json2, JsonNode.class);

        JSONObject jsonObject = JSONObject.parseObject(node.path("icons").toString());
        System.out.println("+++++++++++++++++++++++++++++++++++");
        jsonObject.keySet().forEach(key -> {
            System.out.println("key" + "==" + key);
            System.out.println("content" + "==" + jsonObject.getString(key));
        });

//        String objects = node.path("objects").toString();
//        List list = new ObjectMapper().readValue(objects, List.class);

//        String join = StringUtils.join(list, ",");
//        System.out.println(join);
//        for ( Object v:
//             list) {
//            System.out.println(v);
//        }

//        String icons = node.path("icons").toString();
//        JsonNode jsonNode = node.path("object");
//        String s = jsonNode.asText();
//        if (node.hasNonNull("on")) {
//            System.out.println("hasnonnull - this node is null");
//        }
//        if (node.has("on")) {
//            System.out.println("has - this node is null");
//        }

//        JSONObject object = new ObjectMapper().readValue(icons, JSONObject.class);
//        Map<String, Object> map = new ObjectMapper().readValue(icons, Map.class);
//        for (String key :
//                map.keySet()) {
//            JSONArray jsonArray = object.getJSONArray(key);
//            Object o = jsonArray.get(0);
//            JSONObject jsonObject = (JSONObject) JSON.toJSON(o);
//            String url = jsonObject.getString("url");
//            String language = jsonObject.getString("language");
//            System.out.println(url + "--" + language);
//            System.out.println(map.get(key));
//        }
    }


}

